package com.example.sh_gameox;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.Image;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import static com.example.sh_gameox.PublicFunction.checkResult;
import static com.example.sh_gameox.PublicFunction.getGame1;
import static com.example.sh_gameox.PublicFunction.getGame2;
import static com.example.sh_gameox.PublicFunction.vibrator;

public class GameActivity extends AppCompatActivity implements View.OnClickListener {
    private int[][] contentList = null;
    private LinearLayout tbBox = null;
    private ImageView[][] imageList = null;
    private Button btnPrev,btnReplay,btnNext;
    private TextView tvTitle;
    int id = 0;
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game_simple);
        tbBox = findViewById(R.id.table_box);
        tvTitle = findViewById(R.id.title);
        btnPrev = findViewById(R.id.prev);
        btnReplay = findViewById(R.id.replay);
        btnNext = findViewById(R.id.next);
        btnPrev.setOnClickListener(this);
        btnReplay.setOnClickListener(this);
        btnNext.setOnClickListener(this);

        id = getIntent().getIntExtra("id",1);
        initImage(id);
        initView();
    }
    private void initImage(int id){
        //初始化视图
        if(contentList==null){
            if(id>=5){
                contentList = getGame2(id);
            }else{
                contentList = getGame1(id);
            }
        }
        initTitle();
        imageList = new ImageView[contentList.length][contentList.length];
        for(int i=0;i<contentList.length;i++){
            LinearLayout linearLayout = new LinearLayout(this);
            linearLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            linearLayout.setOrientation(LinearLayout.HORIZONTAL);
            for(int j=0;j<contentList[i].length;j++){
                ImageView imageView = new ImageView(this);
                imageView.setLayoutParams(new LinearLayout.LayoutParams(120, 120));
                imageView.setPadding(10,10,10,10);
                imageView.setBackgroundResource(R.drawable.shape_border);
                imageView.setId(i*contentList.length+j);
                imageView.setTag("click");
                if(contentList[i][j]!=0){
                    imageView.setTag("unClick");
                    imageView.setImageResource(getBitmap(contentList[i][j]));
                }
                imageList[i][j] = imageView;
                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        updateView(v.getId(),v.getTag().toString());

                    }
                });
                linearLayout.addView(imageView);
            }
            tbBox.addView(linearLayout);
        }
//        tbBox.addView(imageView);
    }

    private int getBitmap(int i){
        if(i==1){
            return R.mipmap.icon_x;
        }else{
            return R.mipmap.icon_o;
        }
    }

    /**
     * 上一关
     */
    private void prevLevel(){
        id--;
        initView();
        replay();
    }

    /**
     * 下一关
     */
    private void nextLevel(){
        id++;
        initView();
        replay();
    }

    /**
     * 重玩
     */
    private void replay(){
        tbBox.removeAllViews();
        contentList = null;
        imageList = null;
        initImage(id);

    }
    private void initView(){
        viewShow(btnPrev);
        viewShow(btnNext);
        if(id==1){
            viewGone(btnPrev);
        }
        if(id==6){
            viewGone(btnNext);
        }
    }

    /**
     * 显示成功提示框
     */
    private void showDialog(){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("系统消息")
                .setMessage("恭喜通关成功");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.show();
        disableImage();
    }

    /**
     * 将所有的图片变为不可点击状态
     */
    private void disableImage(){
        for(int i=0;i<imageList.length;i++){
            for(int j=0;j<imageList.length;j++){
                imageList[i][j].setTag("unClick");
            }
        }
    }

    /**
     * 重置棋盘数据
     */
    private void clearList(){

    }
    /**
     * 更新视图
     */
    private void updateView(int id,String click){
        //判断是否可以点击
        if(click.equals("click")){
            //根据ID查出几行几列
            int[] cr = getColAndRow(id);
            if(contentList[cr[0]][cr[1]]==0){
                contentList[cr[0]][cr[1]] = 1;
                imageList[cr[0]][cr[1]].setImageResource(R.mipmap.icon_x);
            }else if(contentList[cr[0]][cr[1]]==1){
                contentList[cr[0]][cr[1]] = 2;
                imageList[cr[0]][cr[1]].setImageResource(R.mipmap.icon_o);
            }else if(contentList[cr[0]][cr[1]]==2){
                contentList[cr[0]][cr[1]] = 0;
                imageList[cr[0]][cr[1]].setImageBitmap(null);
            }
            if(checkResult(contentList)){
                showDialog();
//                Log.e("999","成功");
            }else{
//                Log.e("999","失败");
            }
        }else{
            //不能点击震动一下
            vibrator();
        }
    }
    private int[] getColAndRow(int i){
        int[] ret = new int[2];
        if(i==0){
            ret[0] = 0;
            ret[1] = 0;
        }else{
            ret[0] = i/contentList.length;
            ret[1] = i%contentList.length;
        }
        return ret;
    }
    private void initTitle(){
        tvTitle.setText("第"+id+"关");
    }

    private void viewGone(View view){
        view.setVisibility(View.GONE);
    }
    private void viewShow(View view){
        view.setVisibility(View.VISIBLE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.prev:
                prevLevel();
                break;
            case R.id.next:
                nextLevel();
                break;
            case R.id.replay:
                replay();
                break;
        }
    }
}
